﻿/*
 DotNetNuke - http://www.dotnetnuke.com
 Copyright (c) 2002-2010 by DotNetNuke Corp.

 Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 
 documentation files (the "Software"), to deal in the Software without restriction, including without limitation 
 the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and 
 to permit persons to whom the Software is furnished to do so, subject to the following conditions:

 The above copyright notice and this permission notice shall be included in all copies or substantial portions 
 of the Software.

 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED 
 TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 
 THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF 
 CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
 DEALINGS IN THE SOFTWARE.

*/
using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;

using System.Security;
using System.Web;

using DotNetNuke.Common.Utilities;
using DotNetNuke.Entities.Portals;


namespace DotNetNuke.Modules.UserDefinedTable
{


  public class MakeThumbnail : IHttpHandler
  {
    private string GetContentType(ImageFormat iFormat)
    {
      if (!iFormat.Guid.Equals(ImageFormat.Jpeg.Guid))
      {
        if (iFormat.Guid.Equals(ImageFormat.Gif.Guid))
        {
          return "image/gif";
        }
        if (iFormat.Guid.Equals(ImageFormat.Png.Guid))
        {
          return "image/png";
        }
        if (iFormat.Guid.Equals(ImageFormat.Tiff.Guid))
        {
          return "image/tiff";
        }
        if (iFormat.Guid.Equals(ImageFormat.Bmp.Guid))
        {
          return "image/x-ms-bmp";
        }
      }
      return "image/jpeg";
    }




    public void ProcessRequest(HttpContext context)
    {
      int num2 = 0;
      int num = 0;
      Bitmap image = null;
      Bitmap bitmap = null;
      HttpRequest request = context.Request;
      try
      {
        ImageFormat jpeg;
        if ( !string.IsNullOrEmpty(request.Params["w"]) )
          num2 = int.Parse(request.QueryString["w"]);

        if ( !string.IsNullOrEmpty(request.Params["h"]) )
          num = int.Parse(request.QueryString["h"]);
        
        string filename = context.Server.MapPath(request.Params["image"]);
        PortalSettings currentPortalSettings = PortalController.GetCurrentPortalSettings();
        if (filename.ToLowerInvariant().LastIndexOf(context.Server.MapPath(currentPortalSettings.HomeDirectory).ToLowerInvariant()) == -1)
        {
          throw new SecurityException("Access denied to " + filename + ".");
        }
        string cacheKey = filename + num2.ToString() + "x" + num.ToString();
        object objectValue = (DataCache.GetCache(cacheKey));
        if (objectValue == null)
        {
          image = new Bitmap(filename);
          jpeg = ImageFormat.Jpeg;
          int width = image.Width;
          int height = image.Height;
          if (((num2 > 0) & (num2 < width)) || ((num > 0) & (num < height)))
          {
            int num6;
            int num7;
            double num5 = image.PhysicalDimension.Width / image.PhysicalDimension.Height;
            if (num2 == 0)
            {
              num7 = (int)Math.Round((double)(num * num5));
              num6 = num;
            }
            else if (num == 0)
            {
              num7 = num2;
              num6 = (int)Math.Round((double)(((double)num2) / num5));
            }
            else if ((width / num2) >= (height / num))
            {
              num7 = num2;
              num6 = (int)Math.Round((double)(((double)num2) / num5));
            }
            else
            {
              num7 = (int)Math.Round((double)(num * num5));
              num6 = num;
            }
            bitmap = new Bitmap(num7, num6);
            using (Graphics graphics = Graphics.FromImage(bitmap))
            {
              graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
              graphics.FillRectangle(Brushes.White, 0, 0, num7, num6);
              graphics.DrawImage(image, 0, 0, num7, num6);
            }
            DataCache.SetCache(cacheKey, (bitmap.Clone()));
          }
          else
          {
            bitmap = image;
          }
        }
        else
        {
          bitmap = (Bitmap)((Bitmap)objectValue).Clone();
          jpeg = ImageFormat.Jpeg;
        }
        context.Response.ContentType = this.GetContentType(jpeg);
        bitmap.Save(context.Response.OutputStream, jpeg);
      }
      finally
      {
        if (bitmap != null)
        {
          bitmap.Dispose();
        }
        if (image != null)
        {
          image.Dispose();
        }
      }
    }



    //public bool IsReusable
    //{
    //  get
    //  {
    //    return true;
    //  }
    //}



    bool System.Web.IHttpHandler.IsReusable
    {
      get
      {
        return true;
      }
    }


  }
}

